package com.googlecode.battleofendor.synchroniation;

import com.googlecode.battleofendor.synchroniation.messages.Ping;
import com.googlecode.battleofendor.synchroniation.messages.Timedelta;

public class ClientPlayer extends Player {

	private long hostTimeDelta;
	
	public long currentTimeMillis() {
		return System.currentTimeMillis() + hostTimeDelta;
	}
	
	public void addConnection(final Connection connection) {
		// Auf den Handschlag warten
		System.out.println("Client: Waiting for ping ...");
		Ping request = (Ping) connection.readObject();
		request.pong(this);
		System.out.println("Client: Got ping!");
		
		connection.writeObject(request);
		Timedelta result = (Timedelta) connection.readObject();
		hostTimeDelta = result.getDelta();
		
		// Threads erzeugen
		new Thread(updateMirrorPool(connection)).start();
		new Thread(publishOwnPool(connection)).start();
	}
}
